package com.rusamaha.gm.controllers;


import com.rusamaha.gm.model.ProductBase;
import com.rusamaha.gm.services.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.xml.bind.JAXBException;
import java.io.IOException;


/**
* Created by Mary on 23.02.2015.
*/
@Controller
@RequestMapping("/product")
public class ProductController{

    @Autowired
    ProductService productService;

    @RequestMapping("/save.action")
    public ResponseEntity<ProductBase> saveProduct(@RequestBody ProductBase product) throws IOException, JAXBException {

        //ProductBase product = jsonManager.unMarshall(request.getInputStream(), ProductBase.class);
        //return new ModelAndView(jsonView, ROOT_OBJECT_MAP_KEY, productService.saveOrUpdate(product));
        System.out.print(product);
        return new ResponseEntity<ProductBase>( productService.saveOrUpdate(product), HttpStatus.OK);
    }

//    @RequestMapping(value = "/get/allCustomers.action",
//            headers = "content-type=application/json")
//    public ModelAndView list(HttpServletRequest request,
//                             HttpServletResponse response) {
//
//        Loader loader = null;
//        try {
//            loader = jsonManager.unMarshall(request.getInputStream(),Loader.class);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//        logger.info(loader.toString());
//
//        List<Customer> list = customerService.getCustomers();
//        Map<BaseController,List> map = new HashMap<BaseController,List>();
//        map.put(BaseController.ROOT_OBJECT_MAP_KEY, list);
//        jView.renderMergedOutputModel(map,request,response);
//        return new ModelAndView(jView,"customers",map);
//    }
}
